package com.hxq.library.controller;

import com.hxq.library.service.IUserService;
import com.hxq.library.service.Impl.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author: Ljx
 * @Date: 2021/11/25 20:22
 * @role:
 */
@WebServlet("/changePassword")
public class ChangePasswordServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html,utf-8");
        String pwd = req.getParameter("pwd");
        String pwd1 = req.getParameter("pwd1");
        String code = req.getParameter("code");
        HttpSession session = req.getSession();
        String code1 = (String) session.getAttribute("code");
        if (pwd==""||pwd1==""||code==""){
            req.setAttribute("msg2","输入的值不能为空！！！"+"\n" +"您的验证码为："+code1);
            req.getRequestDispatcher("changePassword.jsp").forward(req,resp);
        }else if (!pwd.equals(pwd1)){
            req.setAttribute("msg2","两次输入的密码不一致！！！"+"\n" +"您的验证码为："+code1);
            req.getRequestDispatcher("changePassword.jsp").forward(req,resp);
        }else if (!code.equals(code1)){
            req.setAttribute("msg2","验证码输入错误！！！"+"\n" +"您的验证码为："+code1);
            req.getRequestDispatcher("changePassword.jsp").forward(req,resp);
        } else {
            String account = (String) session.getAttribute("account1");
            IUserService service = new UserService();
            try {
                boolean changePassword = service.changePassword(account, pwd);
                session.setAttribute("changePassword",changePassword);
                resp.sendRedirect(req.getContextPath()+"booleanChangepwd.jsp");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
